﻿using System.Collections.Generic;
using System.Web.Services;
using CarRent.BL;
using CarRent.BL.Models;
using System;

namespace CarRent.Handler
{
    /// <summary>
    /// Summary description for GetService
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    //[System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    [System.Web.Script.Services.ScriptService]
    public class MyService : System.Web.Services.WebService
    {
        const int PageSize = 20;

        [WebMethod]
        public object GetBookings(string status, int pageNumber, string keyword)
        {
            int count, countPending, countApproval, countDeleted;
            BookingImpl bookingImpl = new BookingImpl();
            var list = bookingImpl.GetBookings(status, keyword, null, null, pageNumber, PageSize, out count, out countPending, out countApproval, out countDeleted);

            ApprovalGridTable<BookingInfo> obj = new ApprovalGridTable<BookingInfo>
            {
                PageNumber = pageNumber,
                RowCount = count,
                PageSize = PageSize,
                List = list,
                CountPending = countPending,
                CountApproval = countApproval,
                CountDeleted = countDeleted
            };

            return obj;
        }

        [WebMethod]
        public object GetBookingList(string status, int pageNumber, string keyword)
        {
            int count, countPending, countApproval, countDeleted;
            BookingImpl bookingImpl = new BookingImpl();
            var list = bookingImpl.GetBookings(status, keyword, null, null, pageNumber, PageSize, out count, out countPending, out countApproval, out countDeleted);

            GridTable<BookingInfo> obj = new GridTable<BookingInfo>
            {
                PageNumber = pageNumber,
                RowCount = count,
                PageSize = PageSize,
                List = list,
                ShowStatus = true
            };

            return obj;
        }

        [WebMethod]
        public BookingInfo GetBooking(int bookingId)
        {
            BookingImpl bookingImpl = new BookingImpl();
            return bookingImpl.GetBooking(bookingId);
        }

        [WebMethod]
        public bool DeleteBookings(IList<int> bookingIds)
        {
            BookingImpl bookingImpl = new BookingImpl();
            return bookingImpl.DeleteBookings(bookingIds, true);
        }
        [WebMethod]
        public bool DestroyBookings(IList<int> bookingIds)
        {
            BookingImpl bookingImpl = new BookingImpl();
            return bookingImpl.DestroyBookings(bookingIds, true);
        }
        [WebMethod]
        public bool UpdateBooking(BookingInfo booking)
        {
            BookingImpl bookingImpl = new BookingImpl();
            return bookingImpl.UpdateBooking(booking);
        }

        [WebMethod]
        public bool UndoBookings(IList<int> bookingIds)
        {
            BookingImpl bookingImpl = new BookingImpl();
            return bookingImpl.DeleteBookings(bookingIds, false);
        }

        [WebMethod]
        public object GetEvents(string start, string end)
        {
            DateTime fromDate = DateTime.Parse(start);
            DateTime toDate = DateTime.Parse(end);
            BookingImpl bookingImpl = new BookingImpl();
            var list = bookingImpl.GetEvents(fromDate, toDate);

            return list;
        }
    }
}

[Serializable]
public class GridTable<T>
{
    public int PageNumber { get; set; }
    public int RowCount { get; set; }
    public int PageSize { get; set; }
    public IList<T> List { get; set; }
    public bool ShowStatus { get; set; }
}

[Serializable]
public class ApprovalGridTable<T> : GridTable<T>
{
    public int CountPending { get; set; }
    public int CountApproval { get; set; }
    public int CountDeleted { get; set; }
}